//
//  ViewController.swift
//  XCCycleBanner
//
//  Created by guoshuai.cheng@holla.world on 07/07/2023.
//  Copyright (c) 2023 guoshuai.cheng@holla.world. All rights reserved.
//

import UIKit
import XCCycleBanner
import SnapKit
import JXPageControl

class ViewController: UIViewController {

    @IBOutlet weak var bannerView: XCCycleBanner!
    
    let photoItems = [
        "0.jpg",
        "1.jpg",
        "2.jpg",
        "3.jpg",
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        bannerView.delegate = self
        bannerView.dataSource = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

extension ViewController: XCCycleDataSource {
    

    
    func xcBanner(banner: XCCycleType) -> (XCCycleCellRegister) {
        return XCCycleCellRegister(type: nil,
                                    reuseIdentifier: "BannerCell",
                                    nib: UINib(nibName: "BannerCell", bundle: Bundle.main))
    }
    
    func xcBanner(numberOfItems banner: XCCycleType)-> Int {
        return self.photoItems.count
    }
    
    func xcBanner(banner: XCCycleType,
                  cellForItemAt index: Int,
                  cell: UICollectionViewCell) -> UICollectionViewCell {
            let tempCell = cell as! BannerCell
            tempCell.layer.cornerRadius = 8
            tempCell.layer.masksToBounds = true
            tempCell.imageView.image = UIImage(named: "\(index).jpg")
            tempCell.msgLabel.text = String(index) + "---来喽来喽,他真的来喽~"
            return tempCell
    }
    
    func xcBanner(banner: XCCycleType,
                  params: XCCycleParams) -> XCCycleParams {
        return params
            .timeInterval(2)
            .cycleWay(.forward)
    }
    
    func xcBanner(banner: XCCycleType,
                  layoutParams: XCCycleLayoutParams) -> XCCycleLayoutParams {
        return layoutParams
            .itemSize(CGSize(width: self.view.bounds.width - 20, height: self.bannerView.bounds.height))
            .itemSpacing(10)
    }
    
    
    func xcBanner(pageControl banner: XCCycleType,
                  numberOfPages: Int,
                  coverView: UIView,
                  builder: XCCyclePageControl) -> XCCyclePageControl? {
        
        let pageControl = JXPageControlFill()
        pageControl.contentMode = .bottomRight
        pageControl.activeColor = UIColor.red
        pageControl.columnSpacing = 8
        builder.pageControl = pageControl
        builder.layout = {
            pageControl.snp.makeConstraints { (maker) in
                maker.left.right.equalTo(coverView)
                maker.top.equalTo(coverView.snp.bottom).offset(0)
                maker.height.equalTo(10)
            }
        }
        return builder
        
    }
    
}

//MARK:- JXBannerDelegate
extension ViewController: XCCycleDelegate {
    
    public func xcBanner(banner: XCCycleType,
                         didSelectItemAt index: Int) {
        print("你点击了---\(index)")
    }
    
    func xcBanner(banner: XCCycleType,
                  center index: Int) {
        print("即将显示---\(index)")
    }
    
}
